layui.use(['element', 'table', 'form', 'layer', 'upload'], function () {
    var element = layui.element;
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var upload = layui.upload;
    //获取创建人id
    var createId = localStorage.getItem('createId');
    $('input[name=createId]').val(createId);
    /*上传图片*/
    var uploadInst = upload.render({
        elem: "#test",
        url: "/upload/uploadFile",
        multiple: true,
        field: "files",
        accept: "images",
        done: function (res, index, upload) {
            var mainImage = res.data.path;
            $('#image').prepend('<img src="' + res.data.path
                + '" height="100px" width="100px" border="1px solid #ccc" class="layui-upload-img uploadImgPreView">');
            $('input[name=mainImage]').val(mainImage);
        },
        progress: function (n, elem, e) {
            element.progress('demo', n + '%');
            if (n == 100) {
                layer.msg('上传完毕', {icon: 1});
            }
        }
    });

    /*商品分类*/
    getGoodsClass();
    function getGoodsClass() {
        $.ajax({
            url: '/goodsTable/getGoodsClass',
            type: 'post',
            dataType: 'json',
            success: function (data) {
                var opt="<select name='classId' id='classId'>";
                opt+="<option value=''>请选择商品分类</option>";
                for(var da in data.data) {
                    var ba=da;
                    da++;
                    opt +="<option value='"+da+ "'>"+data.data[ba].title+"</option>"
                }
                opt+="</select>"
                $("#sort").html(opt);
                //刷新下拉框
                form.render('select');
            }
        });
    }

    /*提交*/
    $('#submit').on('click', function () {
        var title = $('input[name=title]').val();
        var productName = $('input[name=productName]').val();
        var mainImage = $('input[name=mainImage]').val();
        var productDescription = $('textarea[name=productDescription]').val();
        var productLabel = $('textarea[name=productLabel]').val();
        var productCode = $('input[name=productCode]').val();
        var bCode = isNaN(productCode);
        var originalpriceProduct = $('input[name=originalpriceProduct]').val();
        var bprice = isNaN(originalpriceProduct);
        var searchKeywords = $('input[name=searchKeywords]').val();
        if (title == '' | productName == '' | mainImage == '' | productDescription == '' | productLabel == '' |
            productCode==''|originalpriceProduct==''|searchKeywords==''|bCode==true|bprice==true){
            layer.msg("请将信息填写完整");
            return;
        }
        var paramProduct = $('#form').serialize();
        $.ajax({
            url: '/goodsTable/addGoods',
            type: 'post',
            dataType: 'json',
            async: false,
            data: paramProduct,
            success: function (data) {
                alert("添加成功");
                location.reload();
            }
        });
    });
});

//返回
function goBack() {
    parent.$('#Jump').val('商品列表');
    parent.getQuick();
    return;
}
